package com.fei.source;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

import util.XMLDoc;

public class DownLoadSource {
	private final static String PREFIX = "http://source.jd.com";

	public static void main(String[] args) throws Exception {
		String name = "tradeConfigCenter";

		String u = "view-source:http://source.jd.com/app/tradeConfigCenter/blob/master/end-delimiter/src/main/java/com/jd/app/server/common/aop/AliasSwitchAdvice.java";

		XMLDoc xmlDoc = new XMLDoc();
		
		killChrome();
		TimeUnit.SECONDS.sleep(1);
		opChrome();
		TimeUnit.SECONDS.sleep(1);
		moveToInput(u);
		TimeUnit.SECONDS.sleep(1);
		getData(xmlDoc);
	}

	private static void getData(XMLDoc xmlDoc) throws Exception {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Robot robot = new Robot();
		robot.delay(100);
		robot.mouseMove(screenSize.width - 30, screenSize.height / 2);
		robot.delay(1000);
		robot.mousePress(InputEvent.BUTTON1_MASK);
		robot.delay(300);
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
		robot.delay(100);
		
		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.keyPress(KeyEvent.VK_A);

		robot.delay(100);
		robot.keyRelease(KeyEvent.VK_A);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		robot.delay(100);
		
		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.keyPress(KeyEvent.VK_C);

		robot.delay(100);
		robot.keyRelease(KeyEvent.VK_C);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		robot.delay(100);
		
		String data = getSystemClipboard();
		
		xmlDoc.loadFromXMLString(data);
		
		System.out.println(xmlDoc);

	}

	private static void opChrome() throws IOException {
		Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.baidu.com");
	}

	private static void killChrome() {
		try {
			String[] cmd = { "tasklist" };
			Process proc = Runtime.getRuntime().exec(cmd);
			BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
			String string_Temp = in.readLine();
			while (string_Temp != null) {
				if (string_Temp.indexOf("chrome.exe") != -1)
					Runtime.getRuntime().exec("Taskkill /IM chrome.exe");
				string_Temp = in.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void moveToInput(String text) throws Exception {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Robot robot = new Robot();
		robot.delay(1000);
		robot.mouseMove(screenSize.width / 2, 50);
		robot.delay(1000);
		robot.mousePress(InputEvent.BUTTON1_MASK);
		robot.delay(300);
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
		robot.delay(100);

		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.keyPress(KeyEvent.VK_A);

		robot.delay(100);
		robot.keyRelease(KeyEvent.VK_A);
		robot.keyRelease(KeyEvent.VK_CONTROL);

		robot.delay(100);
		robot.keyPress(KeyEvent.VK_BACK_SPACE);
		robot.keyRelease(KeyEvent.VK_BACK_SPACE);
		robot.delay(100);

		setSystemClipboard(text);
		robot.delay(100);

		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.keyPress(KeyEvent.VK_V);

		robot.delay(100);
		robot.keyRelease(KeyEvent.VK_V);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		robot.delay(100);
		
		robot.keyPress(KeyEvent.VK_ENTER);
		
		robot.delay(2000);

	}

	public static void setSystemClipboard(String refContent) {
		// 设置为static是为了直接使用，不用new一个该类的实例即可直接使用,即定义的: 类名.方法名
		String vc = refContent.trim();// 去除字符串的首位空格
		StringSelection ss = new StringSelection(vc);// 创建能传输指定 String 的
														// Transferable

		Clipboard sysClb = null;
		sysClb = Toolkit.getDefaultToolkit().getSystemClipboard();// 获得系统剪切板
		sysClb.setContents(ss, null);// 设定系统剪切板的内容

	}

	public static String getSystemClipboard() {

		// 获取系统剪切板的文本内容[如果系统剪切板复制的内容是文本]
		Clipboard sysClb = null;
		sysClb = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable t = sysClb.getContents(null); // 获取剪切板的内容，不存在则返回null
		// Transferable t =
		// Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
		// //跟上面三行代码一样
		try {

			// 如果剪切板的内容存在并且该内容的类型为文本类型
			if (null != t && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
				String text = (String) t.getTransferData(DataFlavor.stringFlavor);
				return text;
			}
		} catch (UnsupportedFlavorException e) {
			// System.out.println("Error tip: "+e.getMessage());
		} catch (IOException e) {
		} // System.out.println("Error tip: "+e.getMessage());
		return null;
	}
}
